﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ECC_EXT_ImageUtility.ImageLib;
using Microsoft.Office.Interop.Outlook;

namespace ECCSystem
{
    public partial class frm_Ext_RelyPaper : Form
    {

        //private IList<WaterMarkMDO> waterMarkList;
        private Dictionary<string, WaterMarkMDO> waterMarkMdos;

        public string MailNo { get; set; }

        public frm_Ext_RelyPaper()
        {
            InitializeComponent();
            //Init Configuration Items
            waterMarkMdos = this.GetWaterMarkMdos();
            //this.SendMail();
        }

        private void FormInit()
        {
            this.radioButton_RequestForm_Normal.Checked = true;
            this.checkBox_HomeConfirm.Checked = true;
            this.radioButton_PdtType_LDC.Checked = true;
            this.radioButton_Msg_None.Checked = true;
            this.radioButton_PdtStats_Sales.Checked = true;
            this.radioButton_Contact_Home.Checked = true;

            textBox_MSG_POST.Text = "";
            richTextBox_MSG_Content.Text = "";
            textBox_MSG_TEL.Text = "";
            textBox_MSG_Name.Text = "";

            textBox_Contact_Other.Text = "";
            textBox_Contact_Other_Name.Text = "";

            this.richTextBox_Comment.Text =
                "早急に連絡の上、配達おお願いします。";
        }

        private void SendMail()
        {
            var app = new Microsoft.Office.Interop.Outlook.Application();

            //app.Test();

            app.Session.Logon(System.Type.Missing, System.Type.Missing,true, false);
            MAPIFolder vFolder =
            app.Session.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
            var mailItem =
            (MailItem)vFolder.Items.Add();

            //mailItem.To = "GE_Zhang_Linlin@nissen.co.jp";

            //mailItem.To = "chongjun.pei@genpact.com";
            //mailItem.CC = "chongjun.pei@genpact.com";

            mailItem.To = "eCC_chosa@nissen.co.jp";
            mailItem.CC = "TUCSECCDAIRENSVALL@nissen.co.jp";

            mailItem.Subject = "【大連eCC】JP・プラス配達連絡（売上）【顧客対応不要】　の件";

            mailItem.Subject = this.checkMulti(mailItem.Subject);

            if (this.radioButton_RequestForm_Urgent.Checked)
                mailItem.Subject = "【至急】 " + mailItem.Subject;

            
            mailItem.BodyFormat = OlBodyFormat.olFormatPlain;
            mailItem.Body = "お疲れ様です。\n\nご対応をお願いします。 \n\nMCS:" + this.MailNo;

            mailItem.Attachments.Add(PDFOutputPath);
            mailItem.Send();
            app.Session.Logoff();

            MessageBox.Show("邮件已通过Outlook发送，请在Outlook中确认！");
        }

        private Dictionary<string, WaterMarkMDO> GetWaterMarkMdos()
        {
            var waterMarkList = this.GetWaterMarkList();
            var items = new Dictionary<string, WaterMarkMDO>();
            foreach (var waterMarkMDO in waterMarkList)
            {
                items.Add(waterMarkMDO.Name, waterMarkMDO);
            }

            return items;
        }

        //private Dictionary<string, string> GetFormStatus()
        //{
        //    var dictionary = new Dictionary<string, string>();

        //    //住所確認済
        //    if (this.checkBox_HomeConfirm.Checked)
        //        dictionary.Add("HomeConfirm", "");

        //    //商品種類
        //    if (this.radioButton_PdtType_LDC.Checked)
        //        dictionary.Add("PdtType_LDC", "");
        //    else if (this.radioButton_PdtType_NLC.Checked)
        //        dictionary.Add("PdtType_NLC", "");

        //    //MSGの有無
        //    if (radioButton_Msg_None.Checked)
        //        dictionary.Add("Msg_None", "");
        //    else if (radioButton_Msg.Checked)
        //    {
        //        dictionary.Add("Msg", "");
        //        if (!String.IsNullOrEmpty(textBox_MSG_POST.Text))
        //            dictionary.Add("MSG_POST", textBox_MSG_POST.Text);

        //        if (!String.IsNullOrEmpty(richTextBox_MSG_Content.Text))
        //            dictionary.Add("MSG_Content", richTextBox_MSG_Content.Text);

        //        if (!String.IsNullOrEmpty(textBox_MSG_TEL.Text))
        //            dictionary.Add("MSG_TEL", textBox_MSG_TEL.Text);

        //        if (!String.IsNullOrEmpty(textBox_MSG_Name.Text))
        //            dictionary.Add("MSG_Name", textBox_MSG_Name.Text);
        //    }

        //    //商品状態
        //    if (radioButton_PdtStats_Sales.Checked)
        //        dictionary.Add("PdtStats_Sales", "");
        //    else if (radioButton_PdtStats_NoneSales.Checked)
        //        dictionary.Add("PdtStats_NoneSales", "");

        //    //連絡先
        //    if (radioButton_Contact_Home.Checked)
        //        dictionary.Add("Contact_Home", "");
        //    else if (radioButton_Contact_Mobile.Checked)
        //        dictionary.Add("Contact_Mobile", "");
        //    else if (radioButton_Contact_Company.Checked)
        //        dictionary.Add("Contact_Company", "");
        //    else if (radioButton_Contact_MSG.Checked)
        //        dictionary.Add("Contact_MSG", "");
        //    else if (radioButton_Contact_Other.Checked)
        //    {
        //        dictionary.Add("Contact_Other", "");
        //        if (!string.IsNullOrEmpty(textBox_Contact_Other.Text))
        //            dictionary.Add("Contact_Other_Content", textBox_Contact_Other.Text);
        //    }

        //    //特記事項
        //    if (!string.IsNullOrEmpty(richTextBox_Comment.Text))
        //        dictionary.Add("Comment", richTextBox_Comment.Text);

        //    return dictionary;
        //}

        private IList<WaterMarkMDO> GetWaterMarkList()
        {
            var vList = new List<WaterMarkMDO>();

            //
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "HomeConfirm",
                              PositionX = 508,
                              PositionY = 1650,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });

            vList.Add(new WaterMarkMDO()
                          {
                              Name = "PdtType_LDC",
                              PositionX = 1915,
                              PositionY = 1665,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "PdtType_NLC",
                              PositionX = 1585,
                              PositionY = 1665,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Msg_None",
                              PositionX = 940,
                              PositionY = 1660,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Msg",
                              PositionX = 845,
                              PositionY = 1660,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "MSG_POST",
                              PositionX = 110,
                              PositionY = 1760,
                              Enable = false,
                              WaterMarkSrc = null,
                              WaterMarkText = "",
                              RecSize = new Size(0, 0),
                              MinFontSize = float.MaxValue
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "MSG_Content",
                              PositionX = 60,
                              PositionY = 1830,
                              Enable = false,
                              WaterMarkSrc = null,
                              WaterMarkText = "",
                              RecSize = new Size(2200, 240),
                              MinFontSize = 25
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "MSG_TEL",
                              PositionX = 310,
                              PositionY = 1990,
                              Enable = false,
                              WaterMarkSrc = null,
                              WaterMarkText = "",
                              RecSize = new Size(0, 0),
                              MinFontSize = float.MaxValue
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "MSG_Name",
                              PositionX = 1400,
                              PositionY = 1990,
                              Enable = false,
                              WaterMarkSrc = null,
                              WaterMarkText = "",
                              RecSize = new Size(0, 0),
                              MinFontSize = float.MaxValue
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "PdtStats_Sales",
                              PositionX = 90,
                              PositionY = 2130,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "PdtStats_NoneSales",
                              PositionX = 90,
                              PositionY = 2530,
                              Enable = false,
                              WaterMarkSrc = "CheckMark.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                        {
                            Name = "Contact_Home",
                            PositionX = 235,
                            PositionY = 2300,
                            Enable = false,
                            WaterMarkSrc = "Eclipse.ico",
                            WaterMarkText = ""
                        });
            vList.Add(new WaterMarkMDO()
                        {
                            Name = "Contact_Home_None",
                            PositionX = 235,
                            PositionY = 2710,
                            Enable = false,
                            WaterMarkSrc = "Eclipse.ico",
                            WaterMarkText = ""
                        });



            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Contact_Mobile",
                              PositionX = 390,
                              PositionY = 2300,
                              Enable = false,
                              WaterMarkSrc = "Eclipse.ico",
                              WaterMarkText = ""
                          });

            vList.Add(new WaterMarkMDO()
                            {
                                Name = "Contact_Mobile_None",
                                PositionX = 390,
                                PositionY = 2710,
                                Enable = false,
                                WaterMarkSrc = "Eclipse.ico",
                                WaterMarkText = ""
                            });
            

            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Contact_Company",
                              PositionX = 575,
                              PositionY = 2300,
                              Enable = false,
                              WaterMarkSrc = "Eclipse.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                        {
                            Name = "Contact_Company_None",
                            PositionX = 575,
                            PositionY = 2710,
                            Enable = false,
                            WaterMarkSrc = "Eclipse.ico",
                            WaterMarkText = ""
                        });

            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Contact_MSG",
                              PositionX = 790,
                              PositionY = 2300,
                              Enable = false,
                              WaterMarkSrc = "Eclipse.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                            {
                                Name = "Contact_MSG_None",
                                PositionX = 790,
                                PositionY = 2710,
                                Enable = false,
                                WaterMarkSrc = "Eclipse.ico",
                                WaterMarkText = ""
                            });
            
            
            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Contact_Other",
                              PositionX = 1010,
                              PositionY = 2300,
                              Enable = false,
                              WaterMarkSrc = "Eclipse.ico",
                              WaterMarkText = ""
                          });
            vList.Add(new WaterMarkMDO()
                            {
                                Name = "Contact_Other_None",
                                PositionX = 1010,
                                PositionY = 2710,
                                Enable = false,
                                WaterMarkSrc = "Eclipse.ico",
                                WaterMarkText = ""
                            });



            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Contact_Other_Content",
                              PositionX = 1200,
                              PositionY = 2300,
                              Enable = false,
                              WaterMarkSrc = null,
                              WaterMarkText = "",
                              RecSize = new Size(0, 0),
                              MinFontSize = float.MaxValue
                          });

            vList.Add(new WaterMarkMDO()
                        {
                            Name = "Contact_Other_Content_Name",
                            PositionX = 1910,
                            PositionY = 2300,
                            Enable = false,
                            WaterMarkSrc = null,
                            WaterMarkText = "",
                            RecSize = new Size(0, 0),
                            MinFontSize = float.MaxValue
                        });

            vList.Add(new WaterMarkMDO()
                        {
                            Name = "Contact_Other_Content_None",
                            PositionX = 1200,
                            PositionY = 2710,
                            Enable = false,
                            WaterMarkSrc = null,
                            WaterMarkText = "",
                            RecSize = new Size(0, 0),
                            MinFontSize = float.MaxValue
                        });

            vList.Add(new WaterMarkMDO()
                        {
                            Name = "Contact_Other_Content_Name_None",
                            PositionX = 1910,
                            PositionY = 2710,
                            Enable = false,
                            WaterMarkSrc = null,
                            WaterMarkText = "",
                            RecSize = new Size(0, 0),
                            MinFontSize = float.MaxValue
                        });

            vList.Add(new WaterMarkMDO()
                          {
                              Name = "Comment",
                              PositionX = 1040,
                              PositionY = 2850,
                              Enable = false,
                              WaterMarkSrc = null,
                              WaterMarkText = "",
                              RecSize = new Size(1260, 380),
                              MinFontSize = 25
                          });

            return vList;

        }

        private void button_SendMail_Click(object sender, EventArgs e)
        {
            this.StatusInit();

            if(this.Process())
            {
                this.SendMail();
                if(!this.checkBox1.Checked)
                    FormInit();
            }
        }

        private bool Process()
        {
            ImageTools vImageTools = new ImageTools();

            //+
            var replyReportType =
                this.tabControl1.TabPages[0].Text;
            var srcImage = GetBaseImage(replyReportType);
            //-

            foreach (var waterMarkMdo in waterMarkMdos)
            {
                if (waterMarkMdo.Value.Enable)
                {

                    if (waterMarkMdo.Value.WaterMarkSrc != null)
                    {
                        srcImage =
                         vImageTools.CombineImage(srcImage,
                                                  GetWaterMark(waterMarkMdo.Value.WaterMarkSrc),
                                                  waterMarkMdo.Value.PositionX,
                                                  waterMarkMdo.Value.PositionY);
                    }
                    else if (!string.IsNullOrEmpty(waterMarkMdo.Value.WaterMarkText))
                    {
                        srcImage =
                           vImageTools.CombineText(srcImage,
                                                    waterMarkMdo.Value.WaterMarkText,
                                                    waterMarkMdo.Value.PositionX,
                                                    waterMarkMdo.Value.PositionY,
                                                    waterMarkMdo.Value.RecSize,
                                                    waterMarkMdo.Value.MinFontSize);
                    }

                }
            }

            //srcImage.Save(@"c:\2.tif");
            //return false;
            //temp
            //srcImage.Save(@"c:\1.tif");
            openFileDialog1.Filter = @"PDF|*.pdf";
            openFileDialog1.Multiselect = false;
            var res =
                this.openFileDialog1.ShowDialog();

            if(res == DialogResult.Cancel)
                return false;

            int idx =
                this.openFileDialog1.FileName.LastIndexOf("\\");

            var PDFName = this.openFileDialog1.FileName.Substring(idx);

            this.GetOutputPath(PDFName);

            vImageTools.CombinePDFImage(this.openFileDialog1.FileName, srcImage, PDFOutputPath);

            return true;
        }

        private string PDFOutputPath = "";

        private void GetOutputPath(string PDFName)
        {


            var basePath = AppDomain.CurrentDomain.BaseDirectory;

            var vDir = basePath 
                            + @"\ImageLib\BaseSrc\"
                            + DateTime.Now.Ticks.ToString();
            Directory.CreateDirectory(vDir);


            this.PDFOutputPath = vDir + PDFName;

            //return basePath
            //       + @"\ImageLib\BaseSrc\"
            //       + "\\"
            //       + PDFName;
            
        }

        private Image GetBaseImage(string fileName)
        {
            var basePath = AppDomain.CurrentDomain.BaseDirectory;
            return Image.FromFile(basePath + @"\ImageLib\BaseSrc\" + fileName + ".tif");
        }

        private Image GetWaterMark(string fileName)
        {

            var basePath = AppDomain.CurrentDomain.BaseDirectory;
            return Image.FromFile(basePath + @"\ImageLib\BaseSrc\" + fileName);

        }


        private void StatusInit()
        {


            //
            waterMarkMdos["HomeConfirm"].Enable = this.checkBox_HomeConfirm.Checked;

            //
            waterMarkMdos["PdtType_LDC"].Enable = this.radioButton_PdtType_LDC.Checked;
            waterMarkMdos["PdtType_NLC"].Enable = this.radioButton_PdtType_NLC.Checked;

            //
            waterMarkMdos["Msg_None"].Enable = this.radioButton_Msg_None.Checked;
            waterMarkMdos["Msg"].Enable = this.radioButton_Msg.Checked;

            waterMarkMdos["MSG_POST"].Enable = this.radioButton_Msg.Checked;
            waterMarkMdos["MSG_Content"].Enable = this.radioButton_Msg.Checked;
            waterMarkMdos["MSG_TEL"].Enable = this.radioButton_Msg.Checked;
            waterMarkMdos["MSG_Name"].Enable = this.radioButton_Msg.Checked;

            waterMarkMdos["MSG_POST"].WaterMarkText = this.textBox_MSG_POST.Text;
            waterMarkMdos["MSG_Content"].WaterMarkText = this.richTextBox_MSG_Content.Text;
            waterMarkMdos["MSG_TEL"].WaterMarkText = this.textBox_MSG_TEL.Text;
            waterMarkMdos["MSG_Name"].WaterMarkText = this.textBox_MSG_Name.Text;


            //
            waterMarkMdos["PdtStats_Sales"].Enable = this.radioButton_PdtStats_Sales.Checked;
            waterMarkMdos["PdtStats_NoneSales"].Enable = this.radioButton_PdtStats_NoneSales.Checked;

            var bSales = this.radioButton_PdtStats_Sales.Checked;

            waterMarkMdos["Contact_Home"].Enable = this.radioButton_Contact_Home.Checked && bSales;
            waterMarkMdos["Contact_Mobile"].Enable = this.radioButton_Contact_Mobile.Checked && bSales;
            waterMarkMdos["Contact_MSG"].Enable = this.radioButton_Contact_MSG.Checked && bSales;
            waterMarkMdos["Contact_Company"].Enable = this.radioButton_Contact_Company.Checked && bSales;
            waterMarkMdos["Contact_Other"].Enable = this.radioButton_Contact_Other.Checked && bSales;

            waterMarkMdos["Contact_Home_None"].Enable = this.radioButton_Contact_Home.Checked && !bSales;
            waterMarkMdos["Contact_Mobile_None"].Enable = this.radioButton_Contact_Mobile.Checked && !bSales;
            waterMarkMdos["Contact_MSG_None"].Enable = this.radioButton_Contact_MSG.Checked && !bSales;
            waterMarkMdos["Contact_Company_None"].Enable = this.radioButton_Contact_Company.Checked && !bSales;
            waterMarkMdos["Contact_Other_None"].Enable = this.radioButton_Contact_Other.Checked && !bSales;

            waterMarkMdos["Contact_Other_Content"].Enable = waterMarkMdos["Contact_Other"].Enable;
            waterMarkMdos["Contact_Other_Content_None"].Enable = waterMarkMdos["Contact_Other_None"].Enable;
            waterMarkMdos["Contact_Other_Content"].WaterMarkText = this.textBox_Contact_Other.Text;
            waterMarkMdos["Contact_Other_Content_None"].WaterMarkText = this.textBox_Contact_Other.Text;

            waterMarkMdos["Contact_Other_Content_Name"].Enable = waterMarkMdos["Contact_Other"].Enable;
            waterMarkMdos["Contact_Other_Content_Name_None"].Enable = waterMarkMdos["Contact_Other_None"].Enable;
            waterMarkMdos["Contact_Other_Content_Name"].WaterMarkText = this.textBox_Contact_Other_Name.Text;
            waterMarkMdos["Contact_Other_Content_Name_None"].WaterMarkText = this.textBox_Contact_Other_Name.Text;

            //
            waterMarkMdos["Comment"].Enable = true;
            waterMarkMdos["Comment"].WaterMarkText = this.richTextBox_Comment.Text;
        }

        private int _multiCount = 0;

        private String checkMulti(string subjectName)
        {

            if (_multiCount == 0)
            {
                return subjectName;
            }

            if (_multiCount == 1)
                this.checkBox1.Enabled = false;

            var res = _multiCount++
                      + "通目 "
                      + subjectName;

            this.label16.Text = string.Format("当前:{0}", _multiCount);

            return res;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {


            if(this.checkBox1.Checked)
            {
                _multiCount = 1;
                this.label16.Text = string.Format("当前:{0}", _multiCount);
                this.label16.Visible = true;
            }else
            {
                _multiCount = 0;
                this.label16.Text = "";
                this.label16.Visible = false;
            }
        }

        private void frm_Ext_RelyPaper_Load(object sender, EventArgs e)
        {
            //Temp
            this.textBox_MSC.Text = this.MailNo;
        }
    }
}
